import 'package:flutter/material.dart';
import 'package:loading_indicator/loading_indicator.dart';
import 'package:pkg_obz/core/model/page_model.dart';

/// Create： Zhang 3/24/21 5:08 PM
/// 描述：通用组件

///分割线
Container dividerLine({
  double height = 0.3,
  double width = double.infinity,
  double left = 0.0,
  double right = 0.0,
  double top = 0.0,
  double bottom = 0.0,
  color = const Color(0xffCFCFCF),
}) =>
    Container(
      margin: EdgeInsets.only(
        left: left,
        right: right,
        top: top,
        bottom: bottom,
      ),
      height: height,
      width: width,
      color: color,
    );

///CardView控件
Container cardView({
  AlignmentGeometry? alignment,
  EdgeInsetsGeometry? padding,
  EdgeInsetsGeometry? margin,
  Widget? child,
  double? width,
  double? height,
  Color bgColor = const Color(0xffffffff),
  double borderRadius = 4,
  Offset offset = Offset.zero,
  bool isShowShadow = true,
  Color shadowColor = Colors.black38,
}) {
  return Container(
    alignment: alignment,
    padding: padding,
    margin: margin,
    width: width,
    height: height,
    decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(borderRadius),
        boxShadow: isShowShadow
            ? [
                BoxShadow(
                  color: shadowColor,
                  blurRadius: borderRadius,
                  offset: offset,
                )
              ]
            : []),
    child: child,
  );
}

///通用的页面控件,包含四种状态
commonPage({
  required LoadingState state,
  required Widget sucWidget,
  Widget? emptyWidget,
  Widget? errWidget,
  VoidCallback? onFailPageClick,
}) {
  switch (state) {
    case LoadingState.loading:
      return loadingView(
        color: const Color(0xff77B16E),
        margin: const EdgeInsets.all(20),
      );
    case LoadingState.suc:
      return sucWidget;
    case LoadingState.empty:
      return InkWell(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        onTap: onFailPageClick,
        child: emptyWidget ??
            Container(
              alignment: Alignment.center,
              child: const Text("无数据"),
            ),
      );

    case LoadingState.err:
      return InkWell(
        highlightColor: Colors.transparent,
        splashColor: Colors.transparent,
        onTap: onFailPageClick,
        child: errWidget ??
            Container(
              alignment: Alignment.center,
              child: const Text("数据错误"),
            ),
      );
  }
}

///loadingView
loadingView({
  double size = 30,
  Color color = Colors.blue,
  EdgeInsetsGeometry? margin,
}) =>
    Stack(
      alignment: Alignment.topCenter,
      children: [
        Container(
          margin: margin,
          width: double.infinity,
          height: size,
          alignment: Alignment.center,
          child: SizedBox(
            width: size,
            height: size,
            child: LoadingIndicator(
              color: color,
              indicatorType: Indicator.lineScale,
            ),
          ),
        ),
      ],
    );
